﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BusinessLogic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;

namespace DataAccessLogic
{
    public static class AppUserDataAccess
    {
        private static AppUser BindRowToAppUser(DataRow row)
        {
            AppUser au = null;

            if(row != null)
            {
                au = new AppUser();
                au.ID = DataAccess.CheckForGuidColumn(row, "ID");
                au.ConstituentID = DataAccess.CheckForGuidColumn(row, "CONSTITUENTID");
                au.MembershipUser = Membership.GetUser(DataAccess.CheckForGuidColumn(row, "MEMBERSHIPUSERID"));
                au.DateAdded = DataAccess.CheckForDateTimeColumn(row, "DATEADDED");
                au.DateChanged = DataAccess.CheckForDateTimeColumn(row, "DATECHANGED");
                au.AddedByID = DataAccess.CheckForGuidColumn(row, "ADDEDBYID");
                au.ChangedByID = DataAccess.CheckForGuidColumn(row, "CHANGEDBYID");     
                
                if(au.ConstituentID != Guid.Empty)
                {
                    au.Constituent = new Constituent(au.ConstituentID);
                }                          
            }

            return au;
        }

        public static Guid Create(AppUser appUser)
        {
            Guid id = Guid.Empty;
            Guid providerUserKey = new Guid(Membership.GetUser().ProviderUserKey.ToString());
            string outputParameterName = "@ID";

            if (appUser != null)
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                SqlParameter p = new SqlParameter();
                p.ParameterName = outputParameterName;
                p.DbType = System.Data.DbType.Guid;
                p.Direction = System.Data.ParameterDirection.Output;

                parms.Add(p);
                parms.Add(new SqlParameter("@CONSTITUENTID", appUser.ConstituentID));
                parms.Add(new SqlParameter("@USERID", providerUserKey));
                parms.Add(new SqlParameter("@ASPNETDBUSERID", appUser.ASPNetDbUserID));

                object obj = DataAccess.Create(StoredProcedures.USP_APPUSER_CREATE, outputParameterName, parms);

                if (obj != null)
                {
                    id = new Guid(obj.ToString());
                }
            }

            return id;
        }   

        public static void Delete(Guid id)
        {
            if(id != Guid.Empty)
            {
                DataAccess.Delete(StoredProcedures.USP_APPUSER_DELETE, id);
            }
        }

        private static List<AppUser> Get(string storedProcedure)
        {
            return Get(storedProcedure, null);
        }

        private static List<AppUser> Get(string storedProcedure, List<SqlParameter> parms)
        {
            List<AppUser> appUsers = null;
            DataTable dt = DataAccess.Get(storedProcedure, parms);

            if((dt != null) && (0 < dt.Rows.Count))
            {
                appUsers = new List<AppUser>();

                foreach (DataRow row in dt.Rows)
                {
                    appUsers.Add(BindRowToAppUser(row));
                }
            }

            return appUsers;
        }

        public static List<AppUser> GetActive()
        {
            return Get(StoredProcedures.USP_APPUSER_GETACTIVE);
        }

        public static List<AppUser> GetAll()
        {
            return Get(StoredProcedures.USP_APPUSER_GETALL);
        }

        public static AppUser GetByID(Guid id)
        {
            AppUser au = null;

            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ID", id));

            List<AppUser> aus = Get(StoredProcedures.USP_APPUSER_GETBYID, parms);

            if(null != aus && 0 < aus.Count)
            {
                au = aus[0];
            }

            return au;            
        }

        public static string GetNameByID(Guid id)
        {
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ID", id));
            return DataAccess.Get(StoredProcedures.USP_APPUSER_GETNAMEBYID, parms).Rows[0]["NAME"].ToString();
        }
    }
}